package com.csxy.yhj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.csxy.yhj.entity.CareSe;
import com.csxy.yhj.entity.vo.*;
import com.csxy.yhj.service.CareSeService;
import com.csxy.yhj.yhj.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/eduservice/course")
public class CareSeController {
    @Autowired
    private CareSeService careSeService;

    //添加服务基本信息方法
    @RequestMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CareInfoForm courseInfoForm) {
      //根据前端页面去做的一个表单实体类，用于接受前段传回来的对象
        String id = careSeService.addCareInfo(courseInfoForm);
        //回传ID主要是下一步创建章节时候，把章节和服务相关联
        return R.ok().data("courseId",id);
    }

    //查询服务信息
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId){
        CareInfoForm courseInfoForm = careSeService.getCareInfo(courseId);
        return R.ok().data("courseInfoForm",courseInfoForm);
    }

    //查询服务信息列表
    @GetMapping("/getCourseList/")
    public R getCourseList(){
        List<CareSe> list = careSeService.list(null);
        return R.ok().data("list",list);
    }

    //修改服务信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CareInfoForm courseInfoForm){
        careSeService.updateCareInfo(courseInfoForm);
        return R.ok();
    }

    //服务列表中删除服务方法
    @DeleteMapping("/removeCourseById/{id}")
    public R removeCareById(@PathVariable String id){
        boolean flag = careSeService.removeCare(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //多条件分页查询
    @Operation(description = "多条件查询服务带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(@Parameter(name = "page", description = "当前页码", required = true)
                                 @PathVariable Long page,
                                 @Parameter(name = "limit", description = "每页记录数", required = true)
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CareQuery careQuery){//通过封装CourseQuery对象来直接获取数据

    //创建分页page对象
    Page<CareSe> pageParam = new Page<>(page, limit);

    //调用service方法实现多条件分页查询
    careSeService.pageQuery(pageParam, careQuery);

    //获取查询到的数据
    List<CareSe> records = pageParam.getRecords();

    //获取总记录数
    long total = pageParam.getTotal();
    return R.ok().data("total", total).data("rows", records);
    }

    //根据服务id查询服务确认信息
    @GetMapping("/getpublishCourseInfo/{id}")
    public R getpublishCourseInfo(@PathVariable String id) {
        CarePublishVo publishCourseInfo = careSeService.getPublishCareInfo(id);
        return R.ok().data("publishCourse", publishCourseInfo);
    }

    //根据服务id查询服务信息【订单】
    @PostMapping("/getCourseInfoByIdOrder/{courseId}")
    public CareSeVo getCourseInfoByIdOrder(@PathVariable String courseId) {
        CareWebVo courseInfo = careSeService.getBaseCareInfo(courseId);
        CareSeVo eduCourseVo = new CareSeVo();
        BeanUtils.copyProperties(courseInfo, eduCourseVo);
        return eduCourseVo;
    }

    //发布服务
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        CareSe careSe = new CareSe();
        careSe.setId(id);
        careSe.setStatus("Normal");
        careSe.setGmtCreate(new Date());
        careSe.setGmtModified(new Date());
        boolean flag = careSeService.updateById(careSe);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

}
